
package id.isran.kamusindonesiamuna.utils;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.util.Log;
import android.widget.Toast;
import id.isran.kamusindonesiamuna.BuildConfig;

public class Utils {

    public static final String SOUND_INA = "sound/indonesia/";
    public static final String SOUND_MUNA = "sound/muna/";
    private static MediaPlayer mp;

    public static void TRACE(String tag, String args) {
        if (BuildConfig.DEBUG)
            Log.d(tag, args);
    }

    /**
     * method untuk memainkan file audio
     * 
     * @param context context
     * @param name nama file mp3
     */
    public static void playAudio(Context context, String name) {
        try {
            mp = new MediaPlayer();
            AssetFileDescriptor descriptor = context.getAssets().openFd(name + ".mp3");
            mp.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(),
                    descriptor.getLength());
            descriptor.close();
            mp.prepare();
            mp.start();

        } catch (Exception e) {
            Utils.TRACE("Utils", "playAudio: " + e.getMessage());
            Toast.makeText(context, "File not found " + name, Toast.LENGTH_SHORT).show();
        } finally {
            // mp.release();
            // mp = null;
        }
    }
}
